package com.elder.business.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.elder.business.domain.MealPlan;
import com.elder.business.domain.MealPlanDTO;
import com.elder.business.mapper.MealPlanMapper;
import com.elder.business.service.IMealPlanService;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author elder
 * @date 2024-03-11
 */
@Service
public class MealPlanServiceImpl implements IMealPlanService
{
    @Autowired
    private MealPlanMapper mealPlanMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param planId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public MealPlanDTO selectMealPlanByPlanId(Long planId)
    {
        MealPlanDTO dto = new MealPlanDTO();
        MealPlan mealPlan = mealPlanMapper.selectMealPlanByPlanId(planId);
        BeanUtils.copyProperties(mealPlan,dto);
        dto.setPlanId(StringNewUtils.long2Str(mealPlan.getPlanId()));
        dto.setPlanTime(DateUtils.dateStr(mealPlan.getPlanTime()));
        return dto;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @return 【请填写功能名称】
     */
    @Override
    public TableDataInfo selectMealPlanList(String planTime,Integer pageNum,Integer pageSize) {
        Long startTime = null;
        Long endTime = null;
        if(!StringNewUtils.isEmpty(planTime)){
            Long time = DateUtils.getTargetDateSeconds(planTime);
            startTime = DateUtils.getDayStartTime(time);
            endTime = DateUtils.getDayEndTime(time);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<MealPlan> list = mealPlanMapper.selectMealPlanList(startTime,endTime);
        PageInfo<MealPlan> pageInfo = new PageInfo<>(list);
        List<MealPlan> activitiesList = pageInfo.getList();
        List<MealPlanDTO> planDTOList = activitiesList.stream().map(v -> {
            MealPlanDTO dto = new MealPlanDTO();
            BeanUtils.copyProperties(v, dto);
            dto.setPlanId(StringNewUtils.formatStr(v.getPlanId()));
            dto.setPlanTime(DateUtils.dateStr(v.getPlanTime()));
            return dto;
        }).collect(Collectors.toList());
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setRows(planDTOList);
        dataInfo.setTotal(pageInfo.getTotal());
        dataInfo.setCode(200);
        dataInfo.setMsg("success");
        return dataInfo;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @return 结果
     */
    @Override
    public int insertMealPlan(MealPlanDTO dto){
        //判断当前日期是否已存在数据
        Long time = DateUtils.getTargetDateSeconds(dto.getPlanTime());
        Long startTime = DateUtils.getDayStartTime(time);
        Long endTime = DateUtils.getDayEndTime(time);
        MealPlan mealPlanISExist = mealPlanMapper.getMealPlanISExist(startTime, endTime);
        if(Objects.nonNull(mealPlanISExist)){
            return 0;
        }
        MealPlan mealPlan = new MealPlan();
        BeanUtils.copyProperties(dto, mealPlan);
        mealPlan.setPlanId(SnowGenerator.getId());
        mealPlan.setPlanTime(DateUtils.getTargetDateSeconds(dto.getPlanTime()));
        return mealPlanMapper.insertMealPlan(mealPlan);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @return 结果
     */
    @Override
    public int updateMealPlan(MealPlanDTO dto)
    {
        MealPlan plan = mealPlanMapper.selectMealPlanByPlanId(LongUtils.str2Long(dto.getPlanId()));
        Long time = DateUtils.getTargetDateSeconds(dto.getPlanTime());
        Long startTime = DateUtils.getDayStartTime(time);
        Long endTime = DateUtils.getDayEndTime(time);
        MealPlan mealPlanISExist = mealPlanMapper.getMealPlanISExist(startTime, endTime);
        if(Objects.nonNull(mealPlanISExist)&& Objects.equals(dto.getPlanTime(),DateUtils.dateStr(plan.getPlanTime()))){
            return 0;
        }
        MealPlan mealPlan = new MealPlan();
        BeanUtils.copyProperties(dto, mealPlan);
        mealPlan.setPlanId(LongUtils.str2Long(dto.getPlanId()));
        mealPlan.setPlanTime(DateUtils.getTargetDateSeconds(dto.getPlanTime()));
        return mealPlanMapper.updateMealPlan(mealPlan);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param planIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMealPlanByPlanIds(Long[] planIds)
    {
        return mealPlanMapper.deleteMealPlanByPlanIds(planIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param planId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMealPlanByPlanId(Long planId)
    {
        return mealPlanMapper.deleteMealPlanByPlanId(planId);
    }
}
